package org.example.zxk.bk.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import org.example.common.cache.LocalCache;
import org.example.common.config.CommonPage;
import org.example.common.config.CommonResult;
import org.example.common.config.ResultCode;
import org.example.zxk.bk.mapper.BkuserZckMapper;
import org.example.zxk.bk.model.dto.AddUserDTO;
import org.example.zxk.bk.model.dto.QueryUserDTO;
import org.example.zxk.bk.model.pojo.BkUserPOJO;
import org.example.zxk.bk.service.BkuserZckService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class BkuserZckServiceImpl implements BkuserZckService {
    @Resource
    private BkuserZckMapper bkuserZckMapper;
    @Resource
    private LocalCache<Map<String, String>> localCache;

    @Override
    public void create(AddUserDTO dto) {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        BkUserPOJO userPOJP = new BkUserPOJO();
        BeanUtils.copyProperties(dto, userPOJP);
        userPOJP.setPassword(dto.getPassword());
        userPOJP.setUserId(uuid);
        userPOJP.setCreateUser(uuid);
        userPOJP.setCreateTime(new Date());
        userPOJP.setUpdateUser(uuid);
        userPOJP.setIsdel("0");
        bkuserZckMapper.insert(userPOJP);
        Map<String, String> mm = new HashMap<>();
        mm.put("password", dto.getPassword());
        localCache.setLocalCache(dto.getAccount(), mm);
    }

    @Override
    public CommonResult login(QueryUserDTO dto) {
        CommonResult result = new CommonResult();
        Map<String, String> account = localCache.getCache(dto.getAccount());
        String password = "";
        String userId="";
        if (account == null) {
            List<BkUserPOJO> pjo = bkuserZckMapper.queryByAccount(dto);
            if (pjo == null || pjo.size() == 0) {
                result.setMessage("用户不存在");
                result.setCode(ResultCode.FAILED.getCode());
                return result;
            }
            BkUserPOJO obj = pjo.get(0);
            password = obj.getPassword();
            userId=obj.getUserId();
        } else {
            password = account.get("password");
        }
        System.out.println(password);
        if (dto.getPassword().equals(password)) {
            Map<String, String> mm = new HashMap<>();
            String uuid = UUID.randomUUID().toString();
            mm.put("code", uuid);
            mm.put("password", dto.getPassword());
            mm.put("role",dto.getAccount());
            mm.put("userId",userId);
            localCache.setLocalCache(dto.getAccount(), mm);
            result.setMessage(ResultCode.SUCCESS.getMessage());
            result.setCode(ResultCode.SUCCESS.getCode());
            Map<String, String> res = new HashMap<>();
            res.put("code", uuid);
            res.put("key", dto.getAccount());
            result.setData(res);
            return result;
        } else {
            result.setMessage("密码错误");
            result.setCode(ResultCode.FAILED.getCode());
            return result;
        }

    }

    @Override
    public CommonResult queryList(QueryUserDTO dto) {
        if(StrUtil.isBlank(dto.getRole())){
            Map<String,String> mm =localCache.getCache(dto.getKey());
            String role =mm.get("role");
            if(!"admin".equals(role)){
                dto.setUserId(mm.get("userId"));
            }
        }
        CommonResult   result= new CommonResult();
        CommonPage<BkUserPOJO> pages = new CommonPage<>();
        PageHelper.startPage(dto.getPageNum(), dto.getSize());
        List<BkUserPOJO> list = bkuserZckMapper.queryByAccount(dto);
        int totalPage = bkuserZckMapper.selectSum();
        com.github.pagehelper.Page listWithPage = (com.github.pagehelper.Page) list;
        pages.setList(listWithPage);
        pages.setTotal((long) totalPage);
        pages.setTotalPage(totalPage % dto.getSize());
        pages.setPageSize(dto.getSize());
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setData(pages);
        result.setMessage("查询成功");
        return result;
    }

}
